package heima.redPacket;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

public class Main {
    public static void main(String[] args){

        // 生成红包
        int packet_num = 200;
        int small_packet_num = (int) (packet_num * 0.8);
        List<Integer> packets = new ArrayList<>(200);
        Random random = new Random();
        int money;
        for(int i=0; i<packet_num; i++){
            if(i<small_packet_num){
                money = random.nextInt(1, 31);
            } else {
                money = random.nextInt(31, 101);
            }
            packets.add(money);
        }

        // 定义线程池和任务
        Callable<Integer> callable = new GetRedPacket(packets);
        ExecutorService executorService = new ThreadPoolExecutor(
                10,
                10,
                0,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        //启动线程池
        int [][] res = new int[100][2];
        for(int i=1; i<=100; i++){
            res[i-1][0] = i;
            Future<Integer> f = executorService.submit(callable);
            try{
                res[i-1][1] = f.get();
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        // 降序排序显示
        Arrays.sort(res, (o1, o2)-> o2[1] - o1[1]);
        for(int i=0; i<100; i++){
            System.out.println(res[i][0] + "号员工获得红包："+ res[i][1] + "元");
        }

    }
}
